$(document).ready(function () {
    //toastr.error('你有新消息了!', 'tittle',{positionClass: 'toast-bottom-center'})
    //toastr.error('你有新消息了!')
    var temp = new Array();
    $.ajax({
        url: '/shop/getcoupon',
        timeout: 100000,
        type: 'get',
        dataType: 'json',
        data: {},
        success: function (rs) {
            if (rs.errno != 0) {
                toastr.error(rs.errmsg);
                return false;
            }
            $('#coupontable').bootstrapTable({
                search: true,//是否启用搜索
                striped: true,//是否显示行间隔色
                sortable: true,//是否启用排序
                sortOrder: "asc",//排序方式
                pagination: true,//是否显示分页,
                pageSize: 10,//每页的记录行数
                showExport: true,//是否显示导出
                exportDataType: "all",//basic', 'all', 'selected'.
                columns: [{
                    field: 'coupon_id',
                    title: 'ID',
                    sortable: true
                }, {
                    field: 'coupon_name',
                    title: '优惠券名称',
                    sortable: true
                }, {
                    field: 'price',
                    title: '金额',
                    sortable: true
                }, {
                    field: 'base_price',
                    title: '起用金额',
                    sortable: true
                }, {
                    field: 'type',
                    title: '类型',
                    sortable: true
                }, {
                    field: 'description',
                    title: '描述',
                    sortable: true
                }, {
                    field: 'status',
                    title: '状态',
                    sortable: true
                }, {
                    field: 'amount',
                    title: '数量',
                    sortable: true
                }, {
                    field: 'amount_remained',
                    title: '剩余数',
                    sortable: true
                }, {
                    field: 'start_time',
                    title: '开始时间',
                    sortable: true
                }, {
                    field: 'end_time',
                    title: '结束时间',
                    sortable: true
                }],

                data: rs.data
            });

            /*
             $('#toolbar').find('select').change(function () {
             var $table = $('#table');
             console.log($(this).val());
             $table.bootstrapTable('destroy').bootstrapTable({
             exportDataType: $(this).val()
             });
             });
             */

            /*
             $('#coupontable').dataTable({
             responsive: true,
             "data": temp,
             "columns": [
             { "title": "ID" },
             { "title": "名称" },
             { "title": "金额" },
             { "title": "起用价格", "class": "center" },
             { "title": "类型", "class": "center" },
             { "title": "描述" },
             { "title": "状态" },
             { "title": "数量" },
             { "title": "剩余" },
             { "title": "启用", "class": "center" },
             { "title": "结束","class": "center"  },
             ]
             });
             */
        },
        error: function () {
            toastr.error('系统错误');
        }
    });

    $('#starttime').datetimepicker({
        language: 'zh-CN',
        autoclose: true,
        todayHighlight: true,
        pickerPosition: 'top-right'
    });
    $('#endtime').datetimepicker({
        language: 'zh-CN',
        autoclose: true,
        todayHighlight: true,
        pickerPosition: 'top-right'
    });
    var btn = $("#confirm");
    btn.click(function () {
        var flag = true;
        $("#addcoupon").find("input[type='text']").each(function () {
            console.log($(this).val());
            if ($(this).val() == "") {
                flag = false;
                return false;
            }
        })
        console.log(flag)
        if (false === flag) {
            toastr.error('请输入增加优惠券所需要的全部信息');
            return false;
        }
        var name = $('#coupon_name').val();
        var coupon_price = $('#coupon_price').val();
        var coupon_baseprice = $('#coupon_base_price').val();
        var coupon_type = $('#coupon_type').val();
        var coupon_amount = $('#coupon_amount').val();
        var coupon_description = $('#coupon_description').val();
        var coupon_starttime = $('#starttime').val();
        var coupon_endtime = $('#endtime').val();
        $.ajax({
            url: '/shop/addcoupon',
            timeout: 100000,
            type: 'get',
            dataType: 'json',
            data: {
                coupon_name: name,
                price: coupon_price,
                type: coupon_type,
                base_price: coupon_baseprice,
                description: coupon_description,
                amount: coupon_amount,
                start_time: coupon_starttime,
                end_time: coupon_endtime,
            },
            success: function (rs) {
                if (rs.errno != 0) {
                    toastr.error(rs.errmsg);
                }
                if (rs.errno == 0) {
                    toastr.success(rs.errmsg);
                    window.location.reload();
                    $('#myModal').modal('hide');
                }
            },
            error: function () {
                toastr.error('系统错误');
            }
        });
        //alert(test);
    });


});